<template>
  <div
    class="attackingWeapons"
    v-if="!isEmpty(weapon)"
    :style="
      !weapon.followPlayer
        ? `transform: translate(-50%, -50%) rotate(${player.pointAngle}deg);`
        : ''
    "
  >
    <div
      class="content"
      :class="[weapon.className, { attack: player.attacking }]"
    ></div>
  </div>
</template>

<script>
import { getWeaponById } from "@/util/weapon.js";
import { mapState } from "vuex";
import { isEmpty } from "@/util/common.js";

export default {
  data() {
    return {
      weapon: null,
    };
  },
  computed: {
    ...mapState("map", ["player"]),
  },
  methods: {
    isEmpty,
  },
  mounted() {
    this.$nextTick(() => {
      let id = this.player.inventory[this.player.currentInventoryIndex].id;
      this.weapon = getWeaponById(id);
      console.log(this.weapon);
    });
  },
};
</script>

<style scoped>
.attackingWeapons {
  position: absolute;
  width: 30px;
  height: 30px;
  top: 50%;
  left: 50%;
  transform-origin: center center;
}
.content {
  width: 100%;
  height: 100%;
}

.sword.attack {
  position: absolute;
  background: url("~@/assets/image/weapon/sword.png") no-repeat;
  background-size: cover;

  animation: sword 500ms linear;
  transform-origin: left bottom;
}
@keyframes sword {
  0% {
    opacity: 0;
    transform: rotate(-45deg) scale(0);
  }
  10% {
    opacity: 1;
    transform: rotate(-45deg) scale(6);
  }
  40% {
    opacity: 1;
    transform: rotate(-45deg) scale(6);
  }
  50% {
    transform: rotate(45deg) scale(6);
  }
  60% {
    opacity: 1;
    transform: rotate(135deg) scale(6);
  }
  90% {
    opacity: 1;
    transform: rotate(135deg) scale(6);
  }
  100% {
    opacity: 0;
    transform: rotate(135deg) scale(0);
  }
}

.bow.attack {
  position: absolute;
  background: url("~@/assets/image/weapon/bow.png") no-repeat;
  background-size: cover;

  animation: bow 2000ms linear;
  transform-origin: center center;
}
.bow.attack::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background: url("~@/assets/image/weapon/arrow.png") no-repeat;
  background-size: cover;

  animation: arrow 2000ms linear;
  transform-origin: center center;
}
@keyframes bow {
  0% {
    transform: translateX(30px) rotate(135deg) scale(0);
  }
  10% {
    transform: translateX(30px) rotate(135deg) scale(2);
  }

  90% {
    transform: translateX(30px) rotate(135deg) scale(2);
  }
  100% {
    transform: translateX(30px) rotate(135deg) scale(0);
  }
}
@keyframes arrow {
  0% {
    transform: rotate(-90deg) translate(0px, 0px) scale(0);
  }
  20% {
    transform: rotate(-90deg) translate(0px, 0px) scale(1);
  }
  80% {
    opacity: 1;
    transform: rotate(-90deg) translate(-6px, 6px) scale(1);
  }
  100% {
    opacity: 0;
    transform: rotate(-90deg) translate(-6px, 6px) scale(1);
  }
}
</style>